<?php

class SiteController extends Controller {

	public $layout = '//layouts/index2';

	/**
	 * Default admin controller filter.
	 * @return array action filters
	 */
	public function filters() {
		return CMap::mergeArray(parent::filters(), array(
					'domainRedirect'
		));
	}

	/**
	 * Redirects to the login page if the domain name is not propsage.com
	 * @param CFilterChain $filterChain
	 */
	public function filterDomainRedirect($filterChain) {
		if ($this->_apiKey != null) {
			$this->_agency = $this->_apiKey->agency;
			$this->redirect(array('/login'));
		}

		$filterChain->run();
	}

	/**
	 * Site controller access rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules() {
		return array(
			array('allow', // allow all users
				'users' => array('*'),
			),
		);
	}

	/**
	 * Declares class-based actions.
	 */
	public function actions() {
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha' => array(
				'class' => 'CCaptchaAction',
				'backColor' => 0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page' => array(
				'class' => 'CViewAction',
				'layout' => '//layouts/index2',
			),
			'error' => 'common.components.PsErrorAction'
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex() {
		$this->layout = '//layouts/index2';

		$this->redirect(array('/site/page/view/index'));
	}

	/**
	 * Displays the contact page
	 */
	public function actionContact() {
		$model = new ContactForm;
		if (isset($_POST['ContactForm'])) {
			$model->attributes = $_POST['ContactForm'];
			if ($model->validate()) {
				$headers = "From: {$model->email}\r\nReply-To: {$model->email}";
				mail(Yii::app()->params['noreplyEmail'], $model->subject, $model->body, $headers);
				Yii::app()->user->setFlash('contact', 'Thank you for contacting us. We will respond to you as soon as possible.');
				$this->refresh();
			}
		}
		$this->render('contact', array('model' => $model));
	}

	public function actionMaintenance() {
		$this->layout = '//layouts/index2_nomenu';
		$this->render("maintenance", array(
			'message' => Yii::app()->maintenanceMode->message,
			'downtimeMessage' => Yii::app()->maintenanceMode->downtimeMessage,
		));
	}

	public function actionCfc() {
		$this->layout = '//layouts/plain';
		$this->render('cfc');
	}

}
